<%@page import="edu.fpt.stub.CustomerStub"%>
<%@page import="edu.fpt.entity.Customer"%>
<%@page import="java.util.Date"%>
<%@page import="edu.fpt.entity.Room"%>
<%@page import="edu.fpt.stub.RoomStub"%>
<%@page import="java.util.Calendar"%>
<%@page import="java.util.StringTokenizer"%>
<%@page import="edu.fpt.entity.RomType"%>
<%@page import="edu.fpt.stub.RoomTypeStub"%>
<%@page import="edu.fpt.entity.Services"%>
<%@page import="edu.fpt.stub.ServiceStub"%>
<%@page import="java.util.List"%>
<%@page import="edu.fpt.stub.LoginStub"%>
<%@page import="edu.fpt.entity.Employee"%>
<%@page import="edu.fpt.entity.BillAll"%>
<%@page import="edu.fpt.stub.BillStub"%>
<%-- 
    Document   : Bill
    Created on : Jan 23, 2013, 11:10:23 PM
    Author     : TuanAnh
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="stylesheet" href="css/modern.css" type="text/css" />  
        <title>Order Form</title>
    </head>
    <body style="color: darkred; " bgcolor="#e4e3e3">  
        <%
            session = request.getSession();
            String user1 = (String) session.getAttribute("id");
            if (user1 == null) {
                response.sendRedirect("LoginForm.jsp");
            } else {

        %>
        <div>
            <h1 style="color: #4D1124; margin-left: 400px">Bill Information</h1>

        </div>
        <div class="progress-bar">
            <div class="bar bg-color-blue" style="width: 33%"></div>
            <div class="bar bg-color-yellow" style="width: 33%"></div>
            <div class="bar bg-color-green" style="width: 34%"></div>
        </div>
        <div style="margin-top: 30px"></div>
        <form name="order" method="post">
            <table class="">
                <thead>
                    <tr style="background-color: #85373b"  >
                        <th style="color: white; text-align: center; width: 140px; font-size: 17px">Bill ID</th>
                        <th style="color: white; text-align: center; width: 190px; font-size: 17px">Date Come</th>
                        <th style="color: white; text-align: center; width: 190px; font-size: 17px">To Day</th>
                        <th style="color: white; text-align: center; width: 250px; font-size: 17px">Services</th>
                        <th style="color: white; text-align: center; width: 130px; font-size: 17px">Number People</th>
                        <th style="color: white; text-align: center; width: 150px; font-size: 17px">Room</th>
                        <th style="color: white; text-align: center; width: 150px; font-size: 17px">Price</th>
                        <th style="color: white; text-align: center; width: 160px; font-size: 17px">Employee Name</th>
                    </tr>
                    <%
                        BillStub bill = new BillStub();
                        List<BillAll> lst = bill.getBill();
                        String id = request.getParameter("dataName");
                        for (BillAll bi : lst) {
                            if (id.equals(bi.getReserDetailId().getCustomerId().getCustomerId())) {
                    %>
                    <tr style="background-color: #dfb7b7">
                        <th style="color: #8e1f1f; text-align: center; width: 140px"><%= id%></th>
                        <th style="color: #8e1f1f; text-align: center; width: 190px"><%= bi.getDateCome()%></th>
                        <%
                            Date d = new Date();
                            String current = String.valueOf(d.getYear() + 1900) + "-" + String.valueOf(d.getDate()) + "-" + String.valueOf(d.getMonth() + 1);
                        %>
                        <th style="color: #8e1f1f; text-align: center; width: 190px"><%= current%></th>
                        <%
                            ServiceStub serviceStub = new ServiceStub();
                            List<Services> ser = serviceStub.getService();
                            for (Services s : ser) {
                                if (s.getServiceId().equals(bi.getReserDetailId().getServiceId().getServiceId())) {
                        %>
                        <th style="color: #8e1f1f; text-align: center; width: 250px"><%= s.getServiceName()%></th>
                        <%
                                }
                            }%>
                        <th style="color: #8e1f1f; text-align: center; width: 130px"><%= bi.getNumberPeople()%></th>
                        <%

                            RoomStub rs = new RoomStub();
                            List<Room> rl = rs.getRoomType();
                            for (Room rid : rl) {
                                if (rid.getRoomID().equals(bi.getReserDetailId().getRoomId().getRoomID())) {


                        %>
                        <th style="color: #8e1f1f; text-align: center; width: 150px"><%= rid.getRoomID()%></th>
                        <th style="color: #8e1f1f; text-align: center; width: 150px"><%=  rid.getRoomType().getPrice()%></th>
                        <%
                                }
                            }%>
                        <%
                            LoginStub employ = new LoginStub();
                            List<Employee> em = employ.getALlEmp();
                            for (Employee e : em) {
                                if (e.getEmployeeId().equals(user1)) {
                        %>
                        <th style="color: #8e1f1f; text-align: center; width: 160px"><%= e.getEmployeeName()%></th>
                        <%
                                }
                            }%>
                    </tr> 
                    <%
                            }
                        }
                    %>
                </thead>
            </table>
            <div style="margin-top: 20px"></div>
            <div class="input-control" style="margin-left: 100px"> Date out: <input type="text" name="date" placeholder="Date Out" style="width:230px" required onkeypress="return isNumberKey(event)" /></div>
            <div style="margin-top: 20px"></div>
            <div style="margin-left: 500px"><input type="submit" value="Payment" name="submit" style="background-color: #0865c2" /><span><input type="button" value="Reset" style="background-color: #e3a21a; color: white;"/></span><span><input type="button" value="Go Back" style="background-color: #47bf4e; color: white;" onclick="window.location.href='CustomerForm.jsp'"/></span></div>
        </form>
        <%
            if (request.getParameter("submit") != null) {
                try {

                    RoomTypeStub roomType = new RoomTypeStub();
                    List<RomType> romtp = roomType.getRoomType();
                    int rom = 0;
                    int serv = 0;
                    //Get Room Price,
                    for (BillAll bi : lst) {
                        if (id.equals(bi.getReserDetailId().getCustomerId().getCustomerId())) {
                            RoomStub rs = new RoomStub();
                            List<Room> rl = rs.getRoomType();
                            for (Room rid : rl) {
                                if (rid.getRoomID().equals(bi.getReserDetailId().getRoomId().getRoomID())) {
                                    rom = rid.getRoomType().getPrice();
                                }
                            }
                            // Services Price
                            ServiceStub serviceStub = new ServiceStub();
                            List<Services> ser = serviceStub.getService();
                            for (Services s : ser) {
                                if (s.getServiceId().equals(bi.getReserDetailId().getServiceId().getServiceId())) {
                                    serv = s.getServicePrice();
                                }
                            }
                        }
                    }
                    //Date Come

                    int tg1, tg2, a, b;
                    String come[] = bill.getDateCome(id).split("[-]");

                    if (Integer.parseInt(come[2]) == 1 || Integer.parseInt(come[2]) == 3 || Integer.parseInt(come[2]) == 5
                            || Integer.parseInt(come[2]) == 7 || Integer.parseInt(come[2]) == 8 || Integer.parseInt(come[2]) == 10
                            || Integer.parseInt(come[2]) == 12) {
                        tg1 = Integer.parseInt(come[2]) * 31;
                    } else if (Integer.parseInt(come[2]) == 2) {
                        if (Integer.parseInt(come[0]) % 100 != 0 && Integer.parseInt(come[0]) % 4 == 0) {
                            tg1 = Integer.parseInt(come[2]) * 29;
                        } else {
                            tg1 = Integer.parseInt(come[2]) * 28;
                        }
                    } else {
                        tg1 = Integer.parseInt(come[2]) * 30;
                    }

                    if (Integer.parseInt(come[0]) % 100 != 0 && Integer.parseInt(come[0]) % 4 == 0) {
                        tg2 = Integer.parseInt(come[0]) * 366;
                    } else {
                        tg2 = Integer.parseInt(come[0]) * 365;
                    }

                    a = Integer.parseInt(come[1]) + tg1 + tg2;
                    //Date Out

                    String da = request.getParameter("date");
                    String outd[] = da.split("[-]");

                    if (Integer.parseInt(outd[2]) == 1 || Integer.parseInt(outd[2]) == 3 || Integer.parseInt(outd[2]) == 5
                            || Integer.parseInt(outd[2]) == 7 || Integer.parseInt(outd[2]) == 8 || Integer.parseInt(outd[2]) == 10
                            || Integer.parseInt(outd[2]) == 12) {

                        tg1 = Integer.parseInt(outd[2]) * 31;
                    } else if (Integer.parseInt(outd[2]) == 2) {
                        if (Integer.parseInt(outd[0]) % 100 != 0 && Integer.parseInt(outd[0]) % 4 == 0) {
                            tg1 = Integer.parseInt(outd[2]) * 29;
                        } else {
                            tg1 = Integer.parseInt(outd[2]) * 28;
                        }
                    } else {
                        tg1 = Integer.parseInt(outd[2]) * 30;
                    }

                    if (Integer.parseInt(outd[0]) % 100 != 0 && Integer.parseInt(outd[0]) % 4 == 0) {
                        tg2 = Integer.parseInt(outd[0]) * 366;
                    } else {
                        tg2 = Integer.parseInt(outd[0]) * 365;
                    }

                    b = Integer.parseInt(outd[1]) + tg1 + tg2;
                    //Total
                    int total = (b - a) * rom + serv;

                    for (BillAll bi : lst) {
                        if (id.equals(bi.getReserDetailId().getCustomerId().getCustomerId())) {
                            CustomerStub cs = new CustomerStub();
                            List<Customer> cs1 = cs.getAllCustomer();
                            for (Customer c : cs1) {
                                if (bi.getBillId().equals(c.getCustomerId())) {
                                    bi.setDatePay(da);
                                    bi.setTotal(total);

                                    bill.updateBill(bi);
                                    RoomStub rs = new RoomStub();
                                    List<Room> lr = rs.getRoomType();
                                    for (Room rm : lr) {
                                        if (bi.getReserDetailId().getRoomId().getRoomID().equals(rm.getRoomID())) {
                                            rm.setReservation(false);
                                            rm.setReceived(true);

                                            rs.updateRoom(rm);
                                            //out.println("<h3 style='color: red; margin-left: 470px;'>Reservation Success!</h3>");
        %>
        <h3 style="margin-left: 600px; color: red">Bill Total</h3>
        <table border="2" style="width: 350px; margin-left: 470px">
            <tr style="height: 30px">
                <td style="background-color: #ae3637; color: white">Customer ID</td>
                <td><%= bi.getReserDetailId().getCustomerId().getCustomerName()%></td>
            </tr>
            <tr style="height: 30px">
                <td style="background-color: #ae3637; color: white">Date Come</td>
                <td><%= bi.getDateCome()%></td>
            </tr>
            <tr style="height: 30px">
                <td style="background-color: #ae3637; color: white">Date Out</td>
                <td><%= bi.getDatePay()%></td>
            </tr>
            <tr style="height: 30px">
                <td style="background-color: #ae3637; color: white">Number Day</td>
                <td><%= b - a%></td>
            </tr>
            <%
                ServiceStub serviceStub = new ServiceStub();
                List<Services> ser = serviceStub.getService();
                for (Services s : ser) {
                    if (s.getServiceId().equals(bi.getReserDetailId().getServiceId().getServiceId())) {
            %>
            <tr style="height: 30px">
                <td style="background-color: #ae3637; color: white">Services</td>
                <td><%= s.getServiceName()%></td>
            </tr>
            <tr style="height: 30px">
                <td style="background-color: #ae3637; color: white">Price Services</td>
                <td><%= s.getServicePrice()%></td>
            </tr>
            <%                     }
                }
            %>  

            <tr style="height: 40px; text-align: center; ">
                <td style="background-color: #8e1f1f; color: white; font-size: 20px">Total:</td>
                <td style="background-color: #8e1f1f; color: white; text-align: center; font-size: 20px"><%= bi.getTotal()%>&nbsp;VND</td>
            </tr>
        </table>

        <%
                                        }
                                    }
                                }
                            }
                        }
                    }

                } catch (Exception ex) {
//                    ex.printStackTrace();
                    out.println("<h3 style='color: red; margin-left: 470px;'>Failure to compute Total.</h3>");
                }
            }
        %>
        <% }%>        
    </body>
</html>
